package com.company;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Test {
    private static String AFILEPATH = "D:\\a.txt";
    private static String BFILEPATH = "D:\\b.txt";

    public static void main(String[] args) {
        if (check(AFILEPATH, BFILEPATH)) {
            System.out.println("FILE OK");
        } else {
            System.out.println("FILE BAD");
        }
    }

    private static boolean check(String fileA, String fileB) {
        try {
            FileInputStream streamA = new FileInputStream(AFILEPATH);
            FileInputStream streamB = new FileInputStream(BFILEPATH);
            List<String> ans_a = func(streamA);
            List<String> ans_b = func(streamB);
            int size = ans_a.size();
            if (ans_b.size() != size) {
                return false;
            }
            for (int i = 0; i < size; i++) {
                if (!ans_a.get(i).equals(ans_b.get(i))) {
                    return false;
                }
                System.out.println("DEBUG");
                System.out.println(ans_a.get(i));
                System.out.println(ans_b.get(i));
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    private static List<String> func(FileInputStream stream) throws IOException {
        List<String> answer = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        String str;
        while ((str = reader.readLine()) != null) {
            String[] arr = str.split(" ");
            Arrays.sort(arr);
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < arr.length; i++) {
                buffer.append(arr[i]);
            }
            answer.add(buffer.toString());
        }
        return answer;
    }
}
